﻿<%@ Page Language="VB" Debug="true" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.OleDb" %>
<script runat="server">

    Sub Page_Load()
        Executar()
    End Sub
    
    Sub ApplyFilter_Click(Sender As Object, E As EventArgs)
        Executar()
    End Sub
    
    Sub Executar()
        if strsql.Text <> "" then
            Dim ConnectionString As String = ConfigurationSettings.AppSettings("strcnn")
            Dim CommandText As String
    
            CommandText = strsql.Text
    
            Dim myConnection As New OleDbConnection(ConnectionString)
            Dim myCommand As New OleDbCommand(CommandText, myConnection)
    
            myConnection.Open()
    
            strant.Text = strsql.Text
    
            try
                DataGrid1.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
                DataGrid1.DataBind()
            catch ex As Exception
                strant.Text = "<font color='red'><b>String SQL inválida:</b> " & strsql.Text & "<hr>" & ex.Message & "</font>"
            end try
    
        Else
            DataGrid1.DataSource = ""
            DataGrid1.DataBind()
    
            strant.Text = ""
        End if
    End Sub

</script>
<html>
<head>
</head>
<body style="FONT-FAMILY: arial">
    <form runat="server">
        <table bordercolor="navajowhite" cellspacing="0" cellpadding="2" width="750" align="center" bgcolor="floralwhite">
            <tbody>
                <tr>
                    <td valign="center" align="middle" bgcolor="#ffdead" colspan="2">
                        <h2>Busca genérica
                        </h2>
                    </td>
                </tr>
                <tr>
                    <td align="middle">
                        <table border="0">
                            <tbody>
                                <tr>
                                    <td>
                                        String SQL:</td>
                                    <td>
                                        <asp:TextBox Height="400" id="strsql" TextMode="multiline" runat="server" Columns="100"></asp:TextBox>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                    </td>
                                    <td>
                                        <asp:Button id="Button1" onclick="ApplyFilter_Click" runat="server" Text="Executar"></asp:Button>
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                    </td>
                </tr>
                <tr>
                    <td align="middle">
                        <asp:Label id="strant" runat="server"></asp:Label>
                        <br />
                        <br />
                        <asp:datagrid id="DataGrid1" runat="server" EnableViewState="False" ForeColor="Black" BackColor="White" CellPadding="3" GridLines="None" CellSpacing="1">
                            <HeaderStyle font-bold="True" forecolor="white" backcolor="#4A3C8C"></HeaderStyle>
                            <ItemStyle backcolor="#DEDFDE"></ItemStyle>
                        </asp:datagrid>
                    </td>
                </tr>
            </tbody>
        </table>
    </form>
</body>
</html>